package top.kpromise.note.modules.todo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import top.kpromise.common.utils.DateUtils;
import top.kpromise.common.utils.StringUtils;

import java.util.Date;

@Getter
@Setter
@ToString
public class CreateToDo {

    @ApiModelProperty(name = "title", value = "标题")
    private String title;

    private String content;

    @JsonIgnore
    private Long userId;

    @JsonIgnore
    private Date deadline;

    @JsonIgnore
    private Integer priority;

    @JsonIgnore
    private Long orderNum;

    public void parseData() {
        String[] array = title.split("##", 2);
        if (array.length < 2) return;
        title = array[0];
        String todoInfo = array[1].replaceAll(" ", "");
        String[] extrasInfo = todoInfo.split("@");
        int length = extrasInfo.length;
        if (length > 2) {
            orderNum = StringUtils.getLong(extrasInfo[2], 1);
            if (orderNum < 1) orderNum = 1L;
        }
        if (length > 1) {
            priority = StringUtils.getInt(extrasInfo[1], 1);
            if (priority < 1) priority = 1;
            if (priority > 4) priority = 4;
        }
        if (length > 0) {
            setDeadline(extrasInfo[0]);
        }
    }

    private void setDeadline(String dateTime) {
        if (dateTime == null) return;
        int length = dateTime.length();
        String preDate = "";
        if (length == 5) {
            preDate = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
        }
        if (length == 7) {
            preDate = DateUtils.formatDate(new Date(), "yyyy-MM-");
        }
        if (length == 10) {
            preDate = DateUtils.formatDate(new Date(), "yyyy-");
        }
        dateTime = preDate + dateTime;
        if (dateTime.length() == 15) {
            deadline = DateUtils.parseDate(dateTime, "yyyy-MM-ddHH:mm");
        }
    }
}
